/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package axil.framework.error;

import axil.api.Argument;
import axil.api.Formatted;
import axil.api.Formatting;
import axil.api.Persona;
import axil.api.error.AxilCompilationException;
import axil.api.extend.Source;
import axil.engine.Repository;
import axil.etc.NameValue;
import axil.framework.localization.Message;
import axil.framework.localization.MessageContainer;
import axil.framework.localization.Namespace;

import static axil.framework.Functions.*;


/**
 * An implementation of the AxilCompilationException class.
 */
public class CompilationException extends AxilCompilationException
	implements Formatted, MessageContainer {
	private static final Namespace ns = Namespace.axil.ns("compiler");

	private Message message;
	private int source;
	private int line;


	public CompilationException(Source context, String key, NameValue... values) {
		this.message = new Message(ns, key, values);
		this.source = context.source();
		this.line = context.line();
	}


	public CompilationException(Source source, Message message) {
		super();
		this.message = message;
		this.source = source.source();
		this.line = source.line();
	}


	/**
	 * Get a description of the source where the error occurred. For functions
	 * and other forms from a module, the string will contain the module first,
	 * then the name of the function, separated by a slash character. For an
	 * ad hoc script compiled via the Axil.compile() method, the value is simply
	 * "script".
	 *
	 * @return
	 * 	Returns the source of the error.
	 */
	public String source() {
		if (source == 0) {
			return "script";
		}
		return Repository.instance().source(source);
	}


	/**
	 * Get the line in the source where the exception occurred. If the error
	 * did not occur in an area where source information is available, then
	 * zero is returned.
	 *
	 * @return
	 * 	Returns the line in the source. Lines are numbered starting with 1.
	 */
	public int line() {
		return line;
	}


	/**
	 * Get the translation key. The translation key is the key used to look up
	 * the actual translation text. This key may contain '-' characters but
	 * will not contain any whitespace or other punctuation.
	 *
	 * @return
	 * 	Returns the translation key, which is never null or empty.
	 */
	public String key() {
		return message.key();
	}


	/**
	 * Get the optional message values. Values are name/value pairs containing a
	 * key and the raw, unformatted value.
	 *
	 * @return
	 * 	Returns the message values. The array is never null, but may be empty.
	 */
	public Argument[] values() {
		return message.values();
	}


	/**
	 * Get the translatable message contained within this object. The object
	 * returned is never null.
	 */
	public Message message() {
		return message;
	}


	/**
	 * Returns the detail message string of this throwable.
	 */
	public String getMessage() {
		return getLocalizedMessage();
	}


	/**
	 * Creates a localized description of this throwable.
	 */
	public String getLocalizedMessage() {
		return fmt(this);
	}


	/**
	 * Format this object in a locale-friendly manner. The string returned is
	 * a localized representation of this object using the locale and timezone
	 * associated with this persona. The formatting given is never null and
	 * matches formatting suitable for this type.
	 */
	public String format(Persona persona, Formatting formatting) {
		return source() + ", " + line() + ": " +
		       message.format(persona, formatting);
	}


	/**
	 * Return a string representation of this object. The string returned is
	 * never null.
	 */
	public String toString() {
		return single(wrap(getLocalizedMessage()));
	}
}
